package com.mesSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesSystem.dto.ProcessWorkstationResponse;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.Process;
import com.mesSystem.entity.ProductDefinition;
import com.mesSystem.entity.Workstation;
import com.mesSystem.entity.WorkstationType;
import com.mesSystem.service.impl.ProcessRouteService;
import com.mesSystem.service.impl.ProcessWorkstationService;
import com.mesSystem.service.impl.ProductDefinitionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("productDefinition")
public class ProductDefinitionController {
    @Autowired
    ProductDefinitionService productDefinitionService;
    @Autowired
    ProcessWorkstationService processWorkstationService;
    @Autowired
    ProcessRouteService processRouteService;

    /**
     * 定义一个产品
     *
     * @param productDefinition
     * @return
     */
    @PostMapping("increase")
    public Result increase(@RequestBody ProductDefinition productDefinition) {
        log.info(productDefinition.toString());
        // 在产品定义的时候，便创建好产品 工序bom对应的数据
        productDefinitionService.save(productDefinition);
        return Result.ok("创建成功");
    }

    /**
     * 删除该产品定义
     * TODO -- 产品工序BOM表中对应的内容同时删除
     *
     * @param productDefinitionId
     * @return
     */
    @DeleteMapping("delete")
    public Result delete(Long productDefinitionId) {
        productDefinitionService.removeById(productDefinitionId);
        return Result.ok("删除成功");
    }

    /**
     * 分页查询信息
     */
    @GetMapping("query")
    public Result query(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        // 创建分页器对象
        Page<ProductDefinition> Page = new Page<>(page, size);

        // 调用分页查询方法，将分页器传递给service
        productDefinitionService.page(Page, new QueryWrapper<ProductDefinition>().orderByAsc("product_definition_id"));

        // 从分页器对象中获取分页后的数据
        List<ProductDefinition> productDefinitions = Page.getRecords();

        log.info("被查询信息为：{}", productDefinitions);

        // 返回分页结果
        return Result.ok(productDefinitions);
    }

    /**
     * 根据工艺路线id取得工序信息
     */
    @GetMapping("queryProcess")
    public Result queryProcess(Long processRouteId) {
        List<Process> processList = processRouteService.queryProcessByProcessRouteId(processRouteId);
        return Result.ok(processList);
    }


    /**
     * 根据工序信息取得工位类型信息
     * @param processId
     * @return
     */
    @GetMapping("queryWorkstationType")
    public Result queryWorkstationType(Long processId) {
        List<ProcessWorkstationResponse> workstationTypeList = processWorkstationService.queryWorkstationByProcessId(processId);
        return Result.ok(workstationTypeList);
    }

    /**
     * 更改
     */
    @PostMapping("update")
    public Result update(@RequestBody ProductDefinition productDefinition) {
        productDefinitionService.updateById(productDefinition);
        log.info("update:{}", productDefinition);
        return Result.ok("更新成功");
    }


}
